﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Transportr
{
    public class LineDetailsModel : Model
    {
        private Metadata.LineDetailFilter currentFilter;
        private Dictionary<int, ServiceModel.Line> lineSummary;
        private Dictionary<int, ServiceModel.Terms> lineTerms;
        public event UpdatedHandler Updated;

        public ObservableCollection<LineDetailsItemViewModel> Items { get; private set; }

        public bool IsDataLoaded { get; private set; }

        public LineDetailsModel()
        {
            this.Items = new ObservableCollection<LineDetailsItemViewModel>();
            this.lineSummary = new Dictionary<int, ServiceModel.Line>();
            this.lineTerms = new Dictionary<int, ServiceModel.Terms>();
        }

        public void BeginUpdate(Metadata.LineDetailFilter filter)
        {
            IsDataLoaded = false;
            currentFilter = filter;

            if (currentFilter.LineIds != null && currentFilter.LineIds.Count > 0)
            {
                Service.SendRequest(ServiceModel.Function.Lines,
                                    new ServiceModel.Parameters(),
                                    EndUpdateLineSummary, false);
            }
            else
            {
                // TODO: add filter based on LineDetailMetaData
            }
        }

        void EndUpdateLineSummary(object sender, Service.RequestState state)
        {
            foreach (ServiceModel.Line line in state.Response.Lines.Line)
            {
                this.lineSummary[line.lnId] = line;
            }
            Service.SendRequest(ServiceModel.Function.Terms,
                                new ServiceModel.Parameters() { lnId = currentFilter.LineIds },
                                EndUpdateTerms, false);
        }

        void EndUpdateTerms(object sender, Service.RequestState state)
        {
            this.lineTerms[state.Request.Parameters.lnId[0]] = state.Response.Terms;

            Service.SendRequest(ServiceModel.Function.LineInfo,
                                new ServiceModel.Parameters() { lnId = currentFilter.LineIds },
                                EndUpdate, false);
        }

        void EndUpdate(object sender, Service.RequestState state)
        {
            int lnId = state.Request.Parameters.lnId[0];
#if WINDOWS_PHONE
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => {
#endif
                    ObservableCollection<StationViewModel> routeA = new ObservableCollection<StationViewModel>();
                    ObservableCollection<StationViewModel> routeB = new ObservableCollection<StationViewModel>();

                    foreach (ServiceModel.StationDetail sd in state.Response.LineInfo.Stations)
                    {
                        if (sd.slRoute == 0)
                            routeA.Add(new StationViewModel(sd));
                        else if (sd.slRoute == 1)
                            routeB.Add(new StationViewModel(sd));
                    }
                    this.Items.Add(new LineDetailsItemViewModel()
                    {
                        LineIconPath = Icon.Vehicle((ServiceModel.VehicleType)lineSummary[lnId].ltId),
                        Company = lineSummary[lnId].coName,
                        LineNumber = lineSummary[lnId].lnNumber,
                        RouteA = routeA,
                        RouteB = routeB,
                        ReverseRoute = false,
                        CurrentStation = routeA[0],
                        RawTerms = lineTerms[lnId],
                        RawBrief = lineSummary[lnId],
                        RawStations = state.Response.LineInfo
                    });
                    this.IsDataLoaded = true;

                    // kibaszott nagy hack, mert bugos a WP7:
                    // a pivot control itemtemplate-je csak a valtozas utan jeleniti meg az aktualis elemet
                    this.Items.Insert(0, new LineDetailsItemViewModel());
                    this.Items.RemoveAt(0);
                    if (Updated != null) Updated("");
#if WINDOWS_PHONE
                });
#endif
        }
    }
}
